package chapter2;

/**
 * 反转单向链表和双向链表
 */
public class ReverseList {
    /**
     * 反转单向链表，使用一个pre节点和一个next节点辅助，
     * head.next = pre;
     * @param head
     * @return
     */
    public ListNode reverseList(ListNode head) {
//        if (head == null || head.next == null) {
//            return head;
//        }
        ListNode pre = null;
        ListNode next = null;
        while (head != null) {
            next = head.next;
            head.next = pre;
            pre = head;
            head = next;
        }
        return pre;
    }

    /**
     * 反转双向链表，使用一个pre节点和一个next节点辅助，
     * head.next = pre; head.last= next;
     * @param head
     * @return
     */
    public DoubleNode reverseDoubleNode(DoubleNode head) {
        DoubleNode pre = null;
        DoubleNode next = null;
        while (head != null) {
            next = head.next;
            head.next = pre;
            head.last= next;
            pre = head;
            head = next;
        }
        return pre;
    }

}
